/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ipti.br.tag.dao;

import org.ipti.br.tag.entity.*;
import java.io.Serializable;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;


/**
 *
 * @author FelipeMSX
 */
@Stateless
public class MeasureFacade extends AbstractFacade<WeightAndHeight> implements Serializable{
    
    @PersistenceContext(unitName = "TAG2PU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public MeasureFacade() {
        super(WeightAndHeight.class);
    }
          
    public WeightAndHeight findMeasurebyCampaign(long humanID ,long campaignID){
        CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
        CriteriaQuery<WeightAndHeight> cq = cb.createQuery(WeightAndHeight.class);
        Root<WeightAndHeight> from = cq.from(WeightAndHeight.class);
//        Predicate p1 = cb.equal(from.get(Measure_.campaign).get(Campaign_.id), campaignID);
//        Predicate p2 = cb.equal(from.get(Measure_.human).get(Human_.id) ,humanID);
//        cq.where(cb.and(p2,p1));
        
        try{
         getEntityManager().createQuery(cq).getSingleResult();
        }catch(NoResultException e ){
            return null;
        }
        return getEntityManager().createQuery(cq).getSingleResult();
        
    }
}
